package com.yunweiweishi.xuhuanpaltform.yuhuan_app.controller;


import com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.conf.CommonResult;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.pojo.Flights;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.service.FlightsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;


@Api(value = "班次", tags = {"班次controller"})
@RestController
@RequestMapping("/flight")
public class FlightsController {


    @Autowired
    private FlightsService flightsService;


    @Autowired
    private CommonResult result;


    /**
     * 通过巡检的路线的id 查班次
     */

    @ApiOperation( value = "通过班次的id查询班次")
    @GetMapping("/findFlightsByPatrolRouteId")
    public CommonResult findFlightsByPatrolRouteId(@RequestParam("patrolRouteId") Long patrolRouteId) {
        try {

            Optional<Flights> flights = flightsService.findFlightsByPatrolRouteId(patrolRouteId);
            result.setData(flights);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    @ApiOperation( value = "添加班次")
    @PostMapping("/addFlightsTask")
    public CommonResult addFlightsTask(@RequestBody Flights flights
    ) {

        try {
            flightsService.addFlightsTask(flights);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }


    /**
     * 通过id进行更新
     *
     * @param flights
     * @return
     */
    @PutMapping("updateById/{id}")
    public CommonResult updateFlightsById(@RequestBody Flights flights) {

        try {
            flightsService.updateFlightsById(flights);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }

    }


    /**
     * 通过id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteFlightsById")
    public CommonResult deleteFlightsById(@RequestParam("id") Long id) {


        try {
            flightsService.deleteFlightsById(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }

    }

}





